<!-- 蓝色登录页面2 -->
<template>
	<view class="contain">
		<view class="img-a">
			<image src="../../static/logo.png" style="width: 130rpx;height: 130rpx;margin-bottom: 20rpx;"></image></br>
			<image src="../../static/huanying.jpg" style="width: 268rpx;height: 86rpx;"></image>
			<!-- <view class="t-b">
				
				欢迎使用朝阳宝贝
			</view> -->
		</view>
		<view class="login-view" style="">
			<view class="t-login">
				<!-- #ifndef MP-WEIXIN -->
				<u--form :model="form" ref="uForm" :rules="rules">
					<u-form-item borderBottom prop="phone">
						<text class="txt">手机号:</text>
						<u-input type="text" placeholder="请输入您的手机号" v-model="form.phone" />
					</u-form-item>
					<u-form-item borderBottom prop="pwd">
						<text class="txt">密码:</text>
						<u-input type="password" placeholder="请输入您的密码" v-model="form.pwd" />
					</u-form-item>
				</u--form>
				<!-- <view class="t-a">
						<text class="txt">手机号</text>
						<input type="text" name="phone" placeholder="请输入您的手机号" maxlength="11" v-model="phone" />
					</view>
					<view class="t-a">
						<text class="txt">密码</text>
						<input type="password" name="code" maxlength="18" placeholder="请输入您的密码" v-model="pwd" />
					</view> -->
				<view class="t-a">
					<uni-data-checkbox selectedColor="#1cb8a7" v-model="radio1"
						:localdata="logintype"></uni-data-checkbox>
				</view>
				<view class="t-a" style="margin-top: 30px;margin-bottom: 15px;">
					<button @tap="login()">登 录</button>
				</view>
				<!-- #endif -->
				<!-- #ifdef MP-WEIXIN -->
				<view class="w-a" v-if="!radioType">
					<uni-data-checkbox selectedColor="#1cb8a7" v-model="radio1"
						:localdata="logintype"></uni-data-checkbox>
				</view>
				<button style="margin-top: 20px;" open-type="getPhoneNumber"
					@getphonenumber="getPhoneNumber">手机号快捷登录</button>
				<!-- #endif -->
				<!-- <button @click="loginWeixin">微信登录</button> -->
				<!-- <view class="reg" @tap="reg()">注 册</view> -->
				<view class="t-f"><text>—— @2023 新疆塔格智汇电子科技有限公司 ——</text></view>
				<!-- <view class="t-e cl">
					<view class="t-g" @tap="wxLogin()"><image src="@/static/wx.png"></image></view>
					<view class="t-g" @tap="zfbLogin()"><image src="@/static/qq.png"></image></view>
				</view> -->
			</view>
		</view>

		<uni-popup ref="tagsPopup" type="center" :mask-click="false">
			<view class="tags-manager">
				<view class="mb">选择您要进入的学校</view>
				<view class="el-dialog__body">
					<view class="">
						<uni-data-select :localdata="range" @change="roleChange" v-model="tenantId"></uni-data-select>
					</view>
				</view>
			</view>
		</uni-popup>

	</view>
</template>
<script>
	// import { basicUserDeptlist,basicUserDeptpost } from '@/api/school.js'
	export default {

		data() {
			return {
				form: {
					phone: '', //手机号码
					pwd: '', //密码
				},

				radio1: '0',
				logintype: [{
					text: '校园端',
					value: "0"
				}, {
					text: '家长端',
					value: "1"
				}],
				rules: {
					pwd: [
						// 必填规则
						{
							required: true,
							message: '请输入密码',
							// blur和change事件触发检验
							trigger: ['blur', 'change'],
						},
					],
					phone: [{
							required: true,
							message: '请输入手机号',
							trigger: ['change', 'blur'],
						},
						{
							// 自定义验证函数，见上说明
							validator: (rule, value, callback) => {
								// 上面有说，返回true表示校验通过，返回false表示不通过
								// uni.$u.test.mobile()就是返回true或者false的
								return uni.$u.test.mobile(value);
							},
							message: '手机号码不正确',
							// 触发器可以同时用blur和change
							trigger: ['change', 'blur'],
						}
					]
				},
				tenantId: '',
				range: [],
				radioType:''
			};
		},

		onLoad() {
			console.log('login222222',uni.getStorageSync('loginType'))
			// if (JSON.stringify(this.$util.getUser()) != '{}' &&op.loginType==uni.getStorageSync('loginType')) {
			// 	uni.switchTab({
			// 		url: '../tabbar/index/index'
			// 	})
			// } else {
				
			// }
			if(uni.getStorageSync('loginType')){
				this.radioType=uni.getStorageSync('loginType')
				this.radio1=uni.getStorageSync('loginType')
			}
			// this.getuserNew();
		},
		onShow() {
			// uni.clearStorageSync()
		},
		methods: {
			getuserNew() {
				// uni.login({
				// 	provider: 'weixin',
				// 	success: res => {
				// 		console.log('code2222222', res.code);
				// 		uni.request({
				// 			url: 'https://api.weixin.qq.com/sns/jscode2session',
				// 			method: 'GET',
				// 			data: {
				// 				appid: uni.getStorageSync('appid'), //你的小程序的APPID  
				// 				secret: uni.getStorageSync('secret'), //你的小程序的secret,  
				// 				js_code: res.code, //wx.login 登录成功后的code  
				// 				grant_type: 'authorization_code',
				// 			},
				// 			success: (cts) => {
				// 				// 换取成功后 暂存这些数据 留作后续操作  
				// 				this.openId = cts.data.openid //openid 用户唯一标识  
				// 				this.unionid = cts.data
				// 					.unionid //unionid 开放平台唯一标识   当公众号和小程序同时登录过才会有
				// 				this.session_key = cts.data.session_key //session_key  会话密钥  
				// 				console.log(cts)
				// 				console.log(this.openId, this.session_key)
				// 			}
				// 		});
				// 	}
				// });
			},

			getPhoneNumber(e) {
				
				let that = this;
				console.log('获取手机号', e.detail)
				console.log(e.detail.errMsg) // 判断用户是否允许获取手机号
				console.log(e.detail.iv) // 参数 iv
				console.log(e.detail.encryptedData) // 参数encryptedData
				if (e.detail.errMsg == "getPhoneNumber:ok") { // 用户允许或去手机号
					uni.request({
						url: this.$config.URL + "api/admin/wx/user/wxd0e1c1ccdb79b002/phone/",
						method: "GET",
						data: {
							code: e.detail.code
						},
						success: (ress) => {
							console.log('获取手机号', ress)
							if (ress.statusCode == 200) {
								uni.login({
									provider: 'weixin',
									success: res => {
										uni.showLoading({
											title: '登录中'
										}, 2000);
										uni.request({
											url: this.$config.URL +
												"api/admin/wx/user/wxd0e1c1ccdb79b002/login/",
											method: "GET",
											data: {
												code: res.code,
												loginType: uni.getStorageSync(
													'loginType') || this.radio1,
												phone: ress.data.phoneNumber
												// encryptedData: e.detail.encryptedData,
												// iv: e.detail.iv,
												// sessionKey: this.session_key,
												// openId: this.openId,
											},
											header: {
												Authorization: ''
											},
											success: (res) => {
												uni.hideLoading();
												uni.setStorageSync('loginType', this
													.radio1)
												if (res.data.code == "0") {

													if (res.data.data) {
														uni.setStorageSync('isLogin',
															true);
														uni.switchTab({
															url: '../tabbar/index/index'
														})
														this.$util.setUser(res.data
															.data);
														uni.setStorageSync(
															'storage_key', res.data
															.data);
														// #ifndef MP-WEIXIN
														this.cookie.setCookie(
															'Access-Token', res
															.data.data
															.accessToken)
														this.cookie.setCookie(
															'userInfo', JSON
															.stringify(res.data
																.data))
														// #endif
													}
												} else {
													this.$api.showTip(res.data.msg);
													// return uni.showToast({ title: res.data.msg, icon: 'none' });
												}

											}
										})
									}
								})
								// this.form.phone=res.data.phoneNumber
							} else {
								this.$api.showTip('获取手机号失败，请重试')
							}
						}
					})
				}
			},

			// 老师登录
			login() {
				console.log('this.radioq1', this.radio1)
				this.$refs.uForm.validate().then(res => {
					if (this.radio1=='1') {
						uni.showLoading({
							title: '登录中'
						}, 2000);
						uni.request({
							url: this.$config.URL + 'api/app/oauth/v1/parents/login',
							// url: this.$config.URL+'api/app/oauth/v1/username/login',
							data: {
								mobile: this.form.phone,
								pwd: this.form.pwd,
								mac: '',
							},
							method: 'POST',
							contentType: 'application/json',
							success: res => {
								uni.hideLoading();
								if (res.data.code == "0") {
									if (res.data.data) {
										this.$util.setUser(res.data.data);
										uni.setStorageSync('loginType', this.radio1)
										uni.setStorageSync('storage_key', res.data.data);

										uni.reLaunch({
											url: '../tabbar/index/index'
										})
										// #ifndef MP-WEIXIN
										this.cookie.setCookie('Access-Token', res.data.data
											.accessToken)
										this.cookie.setCookie('userInfo', JSON.stringify(res.data
											.data))
										// #endif
									}
								} else {
									this.$api.showTip(res.data.msg);
									// return uni.showToast({ title: res.data.msg, icon: 'none' });
								}
							},
						});
					} else {
						uni.request({
							url: this.$config.URL + 'api/app/basicUserDept/v1/list',
							data: {
								userMobile: this.form.phone,
							},
							method: 'GET',
							contentType: 'application/json',
							success: res => {
								uni.hideLoading();
								if (res.data.code == "0") {
									if (res.data.data.length > 1) {
										res.data.data.forEach((childItem, i) => {
											this.range.push({
												text: childItem.deptName,
												value: childItem.tenantId
											})
										})
										this.$refs.tagsPopup.open();
										//弹框选择


									} else if (res.data.data.length <= 1) {
										this.tenantId = res.data.data.length > 0 ? res.data.data[0]
											.tenantId : '';
										this.staff();
									}

								} else {
									this.$api.showTip(res.data.msg);
								}
							},
						});


					}

				}).catch(errors => {

				})
			},

			roleChange(e) {
				this.tenantId = e;
				if (this.range.length > 0 && this.tenantId) {
					this.staff();
				}
				// console.log('crcrr==========',e)
			},

			staff() {
				uni.showLoading({
					title: '登录中'
				}, 2000);

				uni.request({
					url: this.$config.URL + 'api/app/oauth/v1/staff/login',
					data: {
						mobile: this.form.phone,
						pwd: this.form.pwd,
						tenantId: this.tenantId,
						mac: '',
					},
					method: 'POST',
					contentType: 'application/json',
					success: res => {
						uni.hideLoading();
						this.$refs.tagsPopup.close();
						if (res.data.code == "0") {
							if (res.data.data) {

								uni.setStorageSync('loginType', this.radio1)
								this.$util.setUser(res.data.data);
								uni.setStorageSync('storage_key', res.data.data);

								uni.reLaunch({
									url: '../tabbar/index/index'
								})
								// #ifndef MP-WEIXIN
								this.cookie.setCookie('Access-Token', res.data.data
									.accessToken)
								this.cookie.setCookie('userInfo', JSON.stringify(res.data
									.data))
								// #endif
							}
						} else {
							this.$api.showTip(res.data.msg);
						}
					},
				});
			},


			// 家长登录
			// login() {
			// 	uni.showLoading({
			// 		title: '登录中'
			// 	},2000);
			// 	uni.request({
			// 		url: this.$config.URL+'api/app/oauth/v1/parents/login',
			// 		// url: this.$config.URL+'api/app/oauth/v1/username/login',
			// 		data: {
			// 			mobile: this.phone,
			// 			pwd: this.pwd,
			// 			mac: '',
			// 		},
			// 		method: 'POST',
			// 		contentType: 'application/json',
			// 		success: res => {
			// 			uni.hideLoading();
			// 			if (res.data.code == "0") {
			// 				if(res.data.data){
			// 					uni.switchTab({
			// 						url:'../tabbar/index/index'
			// 					})

			// 					this.$util.setUser(res.data.data);
			// 					// this.$store.commit('userInfo',res.data.data)
			// 					uni.setStorageSync('storage_key', res.data.data);
			// 				}
			// 			} else {
			// 				this.$api.showTip(res.data.msg);
			// 				// return uni.showToast({ title: res.data.msg, icon: 'none' });
			// 			}
			// 		},
			// 	});
			// },
			//注册按钮点击
			reg() {
				uni.showToast({
					title: '注册跳转',
					icon: 'none'
				});
			},
			//等三方微信登录
			wxLogin() {
				uni.showToast({
					title: '微信登录',
					icon: 'none'
				});
			},
			//第三方支付宝登录
			zfbLogin() {
				uni.showToast({
					title: '支付宝登录',
					icon: 'none'
				});
			}
		}
	};
</script>
<style>
	page {
		width: 100%;
		height: 100%;
		background-color: #fff !important;
	}
</style>
<style lang="scss" scoped>
	page {

		.contain {
			width: 100%;
			height: 100%;
			padding-top: 100rpx;
			box-sizing: border-box;
		}
	}

	.txt {
		display: block;
		font-size: 32rpx;
		font-weight: bold;
		margin-bottom: 10rpx;
		margin-right: 20rpx;
		color: $uni-theme-color;
		width: 120rpx;
	}

	.img-a {
		padding-top: 80rpx;
		/* #ifdef MP-WEIXIN*/
		margin-top: 280rpx;
		/* #endif */
		box-sizing: border-box;
		width: 100%;
		height: 450rpx;
		text-align: center;

		image {}

		/* background-image: url(../../static/head.png);
	background-size: 100%; */
	}

	.reg {
		font-size: 28rpx;
		color: #fff;
		height: 90rpx;
		line-height: 90rpx;
		border-radius: 50rpx;
		font-weight: bold;
		background: #f5f6fa;
		color: #000000;
		text-align: center;
		margin-top: 30rpx;
	}

	.login-view {
		width: 100%;
		position: relative;
		margin-top: -145rpx;
		background-color: #ffffff;
		border-radius: 8% 8% 0% 0;
	}

	.t-login {
		width: 600rpx;
		margin: 0 auto;
		font-size: 28rpx;
		padding-top: 80rpx;
		box-sizing: border-box;
	}

	.t-login button {
		font-size: 28rpx;
		background: $uni-theme-color;
		color: #fff;
		height: 90rpx;
		line-height: 90rpx;
		border-radius: 50rpx;
		font-weight: bold;
	}

	.t-login input {
		height: 90rpx;
		line-height: 90rpx;
		margin-bottom: 30rpx;
		border-bottom: 1px solid #e9e9e9;
		font-size: 28rpx;
		border-radius: 12rpx;
	}

	.t-login .t-a {
		position: relative;
		margin-top: 20rpx;
	}

	.t-b {
		text-align: center;
		font-size: 42rpx;
		margin-top: 20rpx;
		color: #444;
		font-weight: 600;
	}

	.t-login .t-c {
		position: absolute;
		right: 22rpx;
		top: 22rpx;
		background: #5677fc;
		color: #fff;
		font-size: 24rpx;
		border-radius: 50rpx;
		height: 50rpx;
		line-height: 50rpx;
		padding: 0 25rpx;
	}

	.t-login .t-d {
		text-align: center;
		color: #999;
		margin: 80rpx 0;
	}

	.t-login .t-e {
		text-align: center;
		width: 250rpx;
		margin: 80rpx auto 0;
	}

	.t-login .t-g {
		float: left;
		width: 50%;
	}

	.t-login .t-e image {
		width: 50rpx;
		height: 50rpx;
	}

	.t-login .t-f {
		position: fixed;
		bottom: 10rpx;
		left: 0;
		width: 100%;
		color: #666;
		text-align: center;
	}

	.t-login .t-f text {
		color: #aaaaaa;
		font-size: 27rpx;
		text-align: center;
	}

	.t-login .uni-input-placeholder {
		color: #aeaeae;
	}

	.cl {
		zoom: 1;
	}

	.cl:after {
		clear: both;
		display: block;
		visibility: hidden;
		height: 0;
		content: '\20';
	}


	.segmented-control {
		width: 217px !important;
	}

	.tags-manager {
		width: 600px;
		padding: 40px 30px;
		border-radius: 5px;
		height: 190px;
		background-color: #fff;
		text-align: center;
	}

	.mb {
		font-size: 40rpx;

	}

	.el-dialog__body {
		text-align: center;
		color: #606266;
		font-size: 14px;
		word-break: break-all;
		padding-top: 20px;
		width: 400px;
		margin: auto;
	}

	.usernameText {
		line-height: 39px;
		margin-left: 20px;
	}
	.w-a{
		margin-bottom: 20px;
		margin-left: 20%;
	}
</style>